<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="80px">
            <el-form-item label="项目名称" prop="propertyName">
              <el-input v-model="queryParams.propertyName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商户编号" prop="doorNumber">
              <el-input v-model="queryParams.doorNumber" placeholder="请输入商户编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商户状态" prop="leaseStatus">
              <el-select v-model="queryParams.leaseStatus" placeholder="请选择商户状态">
                <el-option v-for="dict in property_leasing_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="承&nbsp;&nbsp;租&nbsp;&nbsp;人" prop="tenantName">
              <el-input v-model="queryParams.tenantName" placeholder="请输入承租人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="租赁频次" prop="frentPaymentFrequency">
              <el-select v-model="queryParams.frentPaymentFrequency" placeholder="请选择租赁频次">
                <el-option v-for="dict in land_frequency_of_rent_payments" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="到期提醒">
              <el-select v-model="queryParams.expirationReminder" placeholder="请选择到期提醒">
                <el-option v-for="dict in park_expiration_reminder" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="开始时间">
              <el-date-picker v-model="queryParams.queryStartFromTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择开始日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker v-model="queryParams.queryEndFinishTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <div style="display: flex; justify-content: flex-end; align-items: center">
          <el-button type="warning" icon="Download" @click="handleExport"> 导出 </el-button>
          <el-button plain icon="Plus" @click="handleAdd">新增</el-button>
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="housepropertyInformationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键标识" align="center" prop="id" />
        <el-table-column label="序号" align="center">
          <template #default="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="propertyName" />
        <el-table-column label="商户位置" align="center" prop="propertyLocation" />
        <el-table-column label="商户编号" align="center" prop="doorNumber" />
        <el-table-column label="建筑面积(㎡)" align="center" prop="floorArea" />
        <el-table-column label="商户状态" align="center" prop="leaseStatus">
          <template #default="scope">
            <dict-tag :options="parkbuilingroom_state" :value="scope.row.leaseStatus" />
          </template>
        </el-table-column>
        <el-table-column label="租赁人" align="center" prop="leaseName" />
        <el-table-column label="折合租金(元/月)" align="center" prop="equivalentDailyRent" />
        <el-table-column label="开始时间" align="center" prop="startTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="缴租频次" align="center" prop="frentPaymentFrequency">
          <template #default="scope">
            <dict-tag :options="land_frequency_of_rent_payments" :value="scope.row.frentPaymentFrequency" />
          </template>
        </el-table-column>
        <el-table-column label="到期提醒(近期)" align="center" prop="expirationReminder">
          <template #default="scope">
            <dict-tag :options="park_expiration_reminder" :value="scope.row.expirationReminder" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row.id, scope.row.houseRentalId)"> 编辑 </el-button>
            <el-button link type="primary" @click="handleView(scope.row.id, scope.row.houseRentalId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <HouseFormDialog ref="houseFormDialogRef" @refresh="getList" />
  </div>
</template>

<script setup name="HousepropertyInformation" lang="ts">
import HouseFormDialog from '@/components/HouseFormDialog/index.vue';

import { listHousepropertyInformation, delHousepropertyInformation } from '@/api/zichanguanli/housepropertyInformation/index';
import {
  HousepropertyInformationVO,
  HousepropertyInformationQuery,
  HousepropertyInformationForm
} from '@/api/zichanguanli/housepropertyInformation/types';
import { HosuerentalForm } from '@/api/zichanguanli/hosuerental/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const {
  park_surrender_tenancy_status,
  park_frent_payment_frequency,
  parkbuilingroom_state,
  park_expiration_reminder,
  land_frequency_of_rent_payments
} = toRefs<any>(
  proxy?.useDict('park_surrender_tenancy_status', 'land_frequency_of_rent_payments', 'parkbuilingroom_state', 'park_expiration_reminder')
);

const housepropertyInformationList = ref<HousepropertyInformationVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const housepropertyInformationFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const houseFormDialogRef = ref(null);
const houseFormDialogOpen = (type: string, basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogRef.value.open(type, basicPropertyInformationId, houseRentalId);
};

const initFormData: HousepropertyInformationForm = {
  id: undefined,
  primarySinglet: undefined,
  propertyName: undefined,
  type: undefined,
  propertyLocation: undefined,
  leaseStatus: undefined,
  doorNumber: undefined,
  floorArea: undefined,
  realEstateRegistrationNumber: undefined,
  propertyValuation: undefined,
  remark: undefined,
  appendage: undefined
};

const initHosueRentalFormData: HosuerentalForm = {
  id: undefined,
  name: undefined,
  associate: undefined,
  houseId: undefined,
  contactInformation: undefined,
  leaseholdArea: undefined,
  termContract: undefined,
  startTime: undefined,
  endTime: undefined,
  remainingDays: undefined,
  contractNumber: undefined,
  rentRegulation: undefined,
  appraisedAnnualRent: undefined,
  incrementDate: undefined,
  increasingCountdown: undefined,
  incrementalCriterion: undefined,
  electricityRateStandard: undefined,
  waterTariff: undefined,
  rentOutStatus: undefined,
  rentOutTime: undefined
};
const hoseRentalForm = ref<HosuerentalForm>(initHosueRentalFormData);
hoseRentalForm.value = { ...initHosueRentalFormData };
const data = reactive<PageData<HousepropertyInformationForm, HousepropertyInformationQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    doorNumber: undefined,
    expirationReminder: undefined,
    frentPaymentFrequency: undefined,
    leaseStatus: undefined,
    propertyName: undefined,
    queryEndFinishTime: undefined,
    queryEndFromTime: undefined,
    queryStartFromTime: undefined,
    queryStartToTime: undefined,
    tenantName: undefined
  },
  rules: {
    id: [{ required: true, message: '主键标识不能为空', trigger: 'blur' }],
    primarySinglet: [{ required: true, message: '原单位不能为空', trigger: 'change' }],
    propertyName: [{ required: true, message: '房产名称不能为空', trigger: 'blur' }],
    type: [{ required: true, message: '分类不能为空', trigger: 'change' }],
    propertyLocation: [{ required: true, message: '房产位置不能为空', trigger: 'blur' }],
    doorNumber: [{ required: true, message: '门牌号不能为空', trigger: 'blur' }],
    floorArea: [{ required: true, message: '建筑面积(㎡)不能为空', trigger: 'blur' }],
    realEstateRegistrationNumber: [{ required: true, message: '不动产登记号不能为空', trigger: 'blur' }],
    propertyValuation: [{ required: true, message: '房产估值不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
    appendage: [{ required: true, message: '附件不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询房产(其他)租赁信息列表 */
const getList = async () => {
  console.log(123);
  loading.value = true;
  console.log(queryParams.value);
  const res = await listHousepropertyInformation(queryParams.value);

  housepropertyInformationList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  (queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    doorNumber: undefined,
    expirationReminder: undefined,
    frentPaymentFrequency: undefined,
    leaseStatus: undefined,
    propertyName: undefined,
    queryEndFinishTime: undefined,
    queryEndFromTime: undefined,
    queryStartFromTime: undefined,
    queryStartToTime: undefined,
    tenantName: undefined
  }),
    handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: HousepropertyInformationVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  houseFormDialogOpen('create');
};

/** 修改按钮操作 */
const handleUpdate = async (basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogOpen('edit', basicPropertyInformationId, houseRentalId);
};

const handleView = async (basicPropertyInformationId?: number | string, houseRentalId?: number | string) => {
  houseFormDialogOpen('view', basicPropertyInformationId, houseRentalId);
};

/** 删除按钮操作 */
const handleDelete = async (row?: HousepropertyInformationVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确定删除所选的信息吗？').finally(() => (loading.value = false));
  await delHousepropertyInformation(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'zichanguanli/housepropertyInformation/export',
    {
      ...queryParams.value
    },
    `housepropertyInformation_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
<style scoped>
/* el-table 列数据为空自动显示 -- */
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__wrapper {
  width: 220px;
}
::v-deep .el-form--inline .el-input,
.el-form--inline .el-select {
  width: 220px;
}
</style>
